home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / hplip / ui4 / infodialog.py < prev    next >
Text File  |  2009-10-09  |  11KB  |  311 lines

  1. # -*- coding: utf-8 -*-
  2. #
  3. # (c) Copyright 2001-2008 Hewlett-Packard Development Company, L.P.
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  18. #
  19. # Authors: Don Welch
  20. #
  21.  
  22. # StdLib
  23. import os.path
  24.  
  25. # Local
  26. from base.g import *
  27. from base import device, utils
  28. from prnt import cups
  29. from base.codes import *
  30. from ui_utils import *
  31.  
  32. # Qt
  33. from PyQt4.QtCore import *
  34. from PyQt4.QtGui import *
  35.  
  36. # Ui
  37. from infodialog_base import Ui_Dialog
  38. from deviceuricombobox import DEVICEURICOMBOBOX_TYPE_PRINTER_AND_FAX
  39.  
  40.  
  41. class InfoDialog(QDialog, Ui_Dialog):
  42.     def __init__(self, parent, device_uri):
  43.         QDialog.__init__(self, parent)
  44.         self.device_uri = device_uri
  45.         #self.tabs = []
  46.         self.setupUi(self)
  47.         self.initUi()
  48.  
  49.         QTimer.singleShot(0, self.updateUi)
  50.  
  51.  
  52.     def initUi(self):
  53.         # connect signals/slots
  54.         self.connect(self.CancelButton, SIGNAL("clicked()"), self.CancelButton_clicked)
  55.         self.connect(self.DeviceComboBox, SIGNAL("DeviceUriComboBox_noDevices"), self.DeviceUriComboBox_noDevices)
  56.         self.connect(self.DeviceComboBox, SIGNAL("DeviceUriComboBox_currentChanged"), self.DeviceUriComboBox_currentChanged)
  57.  
  58.         # Application icon
  59.         self.setWindowIcon(QIcon(load_pixmap('hp_logo', '128x128')))
  60.  
  61.         if self.device_uri:
  62.             self.DeviceComboBox.setInitialDevice(self.device_uri)
  63.  
  64.         self.DeviceComboBox.setType(DEVICEURICOMBOBOX_TYPE_PRINTER_AND_FAX)
  65.  
  66.         self.headers = [self.__tr("Key"), self.__tr("Value")]
  67.         self.history_headers = [self.__tr("Date/Time"), None,
  68.                                 self.__tr("Event Code"), self.__tr("Description"),
  69.                                 self.__tr("User"), self.__tr("CUPS Job ID"),
  70.                                 self.__tr("Doc. Title")]
  71.  
  72.  
  73.     def updateUi(self):
  74.         self.DeviceComboBox.updateUi()
  75.         #self.updateInfoTable()
  76.  
  77.  
  78.     def updateInfoTable(self):
  79.         QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
  80.         self.DynamicTableWidget.clear()
  81.         self.DynamicTableWidget.setRowCount(0)
  82.         self.DynamicTableWidget.setColumnCount(0)
  83.         flags = Qt.ItemIsSelectable | Qt.ItemIsEnabled
  84.  
  85.         while self.TabWidget.count() > 3:
  86.             self.TabWidget.removeTab(3)
  87.  
  88.         self.DynamicTableWidget.clear()
  89.         self.DynamicTableWidget.setRowCount(0)
  90.         self.DynamicTableWidget.setColumnCount(len(self.headers))
  91.         self.DynamicTableWidget.setHorizontalHeaderLabels(self.headers)
  92.  
  93.         #
  94.         # Static Data
  95.         #
  96.  
  97.         try:
  98.             d = device.Device(self.device_uri, None)
  99.         except Error:
  100.             QApplication.restoreOverrideCursor()
  101.             FailureUI(self, self.__tr("<b>Unable to open device %1.</b>").arg(self.device_uri))
  102.             #self.close()
  103.             return
  104.  
  105.         self.StaticTableWidget.clear()
  106.  
  107.         self.StaticTableWidget.setColumnCount(len(self.headers))
  108.         self.StaticTableWidget.setHorizontalHeaderLabels(self.headers)
  109.  
  110.         mq_keys = d.mq.keys()
  111.         mq_keys.sort()
  112.  
  113.         self.StaticTableWidget.setRowCount(len(mq_keys))
  114.  
  115.         for row, key in enumerate(mq_keys):
  116.             i = QTableWidgetItem(QString(key))
  117.             i.setFlags(flags)
  118.             self.StaticTableWidget.setItem(row, 0, i)
  119.  
  120.             i = QTableWidgetItem(QString(str(d.mq[key])))
  121.             i.setFlags(flags)
  122.             self.StaticTableWidget.setItem(row, 1, i)
  123.  
  124.         self.StaticTableWidget.resizeColumnToContents(0)
  125.         self.StaticTableWidget.resizeColumnToContents(1)
  126.         self.StaticTableWidget.setSortingEnabled(True)
  127.         self.StaticTableWidget.sortItems(0)
  128.  
  129.         #
  130.         # Dynamic Data
  131.         #
  132.  
  133.         try:
  134.             try:
  135.                 d.open()
  136.                 d.queryDevice()
  137.             except Error, e:
  138.                 QApplication.restoreOverrideCursor()
  139.                 FailureUI(self, self.__tr("<b>Unable to open device %1.</b>").arg(self.device_uri))
  140.                 #self.close()
  141.                 return
  142.  
  143.             dq_keys = d.dq.keys()
  144.             dq_keys.sort()
  145.  
  146.             self.DynamicTableWidget.setRowCount(len(dq_keys))
  147.  
  148.             for row, key in enumerate(dq_keys):
  149.                 i = QTableWidgetItem(QString(key))
  150.                 i.setFlags(flags)
  151.                 self.DynamicTableWidget.setItem(row, 0, i)
  152.  
  153.                 i = QTableWidgetItem(QString(str(d.dq[key])))
  154.                 i.setFlags(flags)
  155.                 self.DynamicTableWidget.setItem(row, 1, i)
  156.  
  157.  
  158.             self.DynamicTableWidget.resizeColumnToContents(0)
  159.             self.DynamicTableWidget.resizeColumnToContents(1)
  160.             self.DynamicTableWidget.setSortingEnabled(True)
  161.             self.DynamicTableWidget.sortItems(0)
  162.  
  163.         finally:
  164.             d.close()
  165.  
  166.         #
  167.         # History Table
  168.         #
  169.  
  170.         self.HistoryTableWidget.clear()
  171.         self.HistoryTableWidget.setRowCount(0)
  172.  
  173.         if d.device_type == DEVICE_TYPE_FAX:
  174.             self.history_headers[1] = self.__tr("Fax")
  175.         else:
  176.             self.history_headers[1] = self.__tr("Printer")
  177.  
  178.         self.HistoryTableWidget.setColumnCount(len(self.history_headers))
  179.         self.HistoryTableWidget.setHorizontalHeaderLabels(self.history_headers)
  180.  
  181.         history = d.queryHistory()
  182.         history.reverse()
  183.         self.HistoryTableWidget.setRowCount(len(history))
  184.  
  185.         for row, h in enumerate(history):
  186.             dt = QDateTime()
  187.             dt.setTime_t(int(h.timedate))
  188.             dt = dt.toString()
  189.  
  190.             ess = device.queryString(h.event_code, 0)
  191.  
  192.             for col, t in enumerate([dt, h.printer_name,
  193.                            unicode(h.event_code), ess,
  194.                            h.username, unicode(h.job_id),
  195.                            h.title]):
  196.  
  197.                 i = QTableWidgetItem(QString(t))
  198.                 i.setFlags(flags)
  199.                 self.HistoryTableWidget.setItem(row, col, i)
  200.  
  201.         self.HistoryTableWidget.resizeColumnToContents(0)
  202.         self.HistoryTableWidget.resizeColumnToContents(1)
  203.         self.HistoryTableWidget.setSortingEnabled(True)
  204.         self.HistoryTableWidget.sortItems(0)
  205.  
  206.         #
  207.         # Printer Data
  208.         #
  209.  
  210.         printers = cups.getPrinters()
  211.  
  212.         for p in printers:
  213.             if p.device_uri == self.device_uri:
  214.                 Tab = QWidget()
  215.                 Tab.setObjectName(QString(p.name))
  216.  
  217.                 GridLayout = QGridLayout(Tab)
  218.                 GridLayout.setObjectName(QString("GridLayout-%s" % p.name))
  219.  
  220.                 Table = QTableWidget(Tab)
  221.                 Table.setAlternatingRowColors(True)
  222.                 Table.setSelectionMode(QAbstractItemView.SingleSelection)
  223.                 Table.setSelectionBehavior(QAbstractItemView.SelectRows)
  224.                 Table.setVerticalScrollMode(QAbstractItemView.ScrollPerItem)
  225.                 Table.setHorizontalScrollMode(QAbstractItemView.ScrollPerPixel)
  226.                 Table.setGridStyle(Qt.DotLine)
  227.                 Table.setObjectName(QString("Table-%s" % p.name))
  228.                 GridLayout.addWidget(Table, 0, 0, 1, 1)
  229.                 self.TabWidget.addTab(Tab, QString(p.name))
  230.  
  231.                 Table.setColumnCount(len(self.headers))
  232.                 Table.setHorizontalHeaderLabels(self.headers)
  233.  
  234.                 cups.resetOptions()
  235.                 cups.openPPD(p.name)
  236.                 current_options = dict(cups.getOptions())
  237.  
  238.                 #current_options['cups_error_log_level'] = cups.getErrorLogLevel()
  239.  
  240.                 try:
  241.                     f = file(os.path.expanduser('~/.cups/lpoptions'))
  242.                 except IOError, e:
  243.                     log.debug(str(e))
  244.                     current_options['lpoptions_file_data'] = QString("(%1)").arg(str(e))
  245.                 else:
  246.                     text = f.read()
  247.                     for d in text.splitlines():
  248.                         if p.name in d:
  249.                             current_options['lpoptions_file_data'] = d
  250.                             break
  251.                     else:
  252.                         current_options['lpoptions_file_data'] = self.__tr("(no data)")
  253.  
  254.                 keys = current_options.keys()
  255.                 keys.sort()
  256.  
  257.                 Table.setRowCount(len(keys))
  258.  
  259.                 for row, key in enumerate(keys):
  260.                     i = QTableWidgetItem(QString(key))
  261.                     i.setFlags(flags)
  262.                     Table.setItem(row, 0, i)
  263.  
  264.                     if key == 'printer-state':
  265.                         state = int(current_options[key])
  266.                         if state == cups.IPP_PRINTER_STATE_IDLE:
  267.                             i = QTableWidgetItem(self.__tr("idle (%1)").arg(state))
  268.                         elif state == cups.IPP_PRINTER_STATE_PROCESSING:
  269.                             i = QTableWidgetItem(self.__tr("busy/printing (%1)").arg(state))
  270.                         elif state == cups.IPP_PRINTER_STATE_STOPPED:
  271.                             i = QTableWidgetItem(self.__tr("stopped (%1)").arg(state))
  272.                         else:
  273.                             i = QTableWidgetItem(QString(str(state)))
  274.                     else:
  275.                         i = QTableWidgetItem(QString(str(current_options[key])))
  276.  
  277.                     i.setFlags(flags)
  278.                     Table.setItem(row, 1, i)
  279.  
  280.                 Table.resizeColumnToContents(0)
  281.                 Table.resizeColumnToContents(1)
  282.                 Table.setSortingEnabled(True)
  283.                 Table.sortItems(0)
  284.  
  285.         cups.closePPD()
  286.         self.TabWidget.setCurrentIndex(0)
  287.         QApplication.restoreOverrideCursor()
  288.  
  289.  
  290.     def DeviceUriComboBox_currentChanged(self, device_uri):
  291.         self.device_uri = device_uri
  292.         self.updateInfoTable()
  293.  
  294.  
  295.     def DeviceUriComboBox_noDevices(self):
  296.         FailureUI(self, self.__tr("<b>No devices found.</b>"))
  297.         self.close()
  298.  
  299.  
  300.     def CancelButton_clicked(self):
  301.         self.close()
  302.  
  303.     #
  304.     # Misc
  305.     #
  306.  
  307.     def __tr(self,s,c = None):
  308.         return qApp.translate("InfoDialog",s,c)
  309.  
  310.  
  311.